package cn.kzgg.ymcc.web.controller;

import cn.kzgg.ymcc.dto.CourseDto;
import cn.kzgg.ymcc.service.ICourseService;
import cn.kzgg.ymcc.domain.Course;
import cn.kzgg.ymcc.query.CourseQuery;
import cn.kzgg.ymcc.result.JSONResult;
import cn.kzgg.ymcc.result.PageList;
import cn.kzgg.ymcc.service.ICourseTypeService;
import cn.kzgg.ymcc.vo.CourseDetailVo;
import cn.kzgg.ymcc.vo.CourseOrderVo;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.sql.Wrapper;

@RestController
@RequestMapping("/course")
public class CourseController {

    @Autowired
    public ICourseService courseService;

    @Autowired
    private ICourseTypeService courseTypeService;

    /**
     *课程订单信息
     * @param courseIds
     * @return
     */
    @RequestMapping(value = "/info/{courseIds}", method = RequestMethod.GET)
    public JSONResult CourseOrderInfo(@PathVariable("courseIds") String courseIds) {
        CourseOrderVo vo = courseService.CourseOrderInfo(courseIds);
        return JSONResult.success(vo);
    }


    /**
     *获取课程详情
     * @param courseId
     * @return
     */
    @RequestMapping(value = "/detail/data/{courseId}", method = RequestMethod.GET)
    public JSONResult CourseDetail(@PathVariable("courseId") Long courseId) {
        CourseDetailVo vo = courseService.CourseDetail(courseId);
        return JSONResult.success(vo);
    }

    /**
     * 课程发布
     * @param courseId
     * @return
     */
    @RequestMapping(value = "/onLineCourse/{courseId}", method = RequestMethod.POST)
    public JSONResult onLineCourse(@PathVariable("courseId") Long courseId) {
        courseService.onLineCourse(courseId);
        return JSONResult.success();
    }

    /**
     * 课程下架
     * @param courseId
     * @return
     */
    @RequestMapping(value = "/offLineCourse/{courseId}", method = RequestMethod.POST)
    public JSONResult offLineCourse(@PathVariable("courseId") Long courseId) {
        courseService.offLineCourse(courseId);
        return JSONResult.success();
    }

    /**
     * 保存和修改公用的
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public JSONResult saveOrUpdate(@RequestBody @Valid CourseDto courseDto) {
        courseService.save(courseDto);
        return JSONResult.success();
    }

    /**
     * 删除对象
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public JSONResult delete(@PathVariable("id") Long id) {
        courseService.deleteById(id);
        return JSONResult.success();
    }

    /**
     * 获取对象
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public JSONResult get(@PathVariable("id") Long id) {
        return JSONResult.success(courseService.selectById(id));
    }


    /**
     * 查询所有对象
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @PreAuthorize("hasAuthority('course:list')")
    public JSONResult list() {
        return JSONResult.success(courseService.selectList(null));
    }


    /**
     * 带条件分页查询数据
     */
    @RequestMapping(value = "/pagelist", method = RequestMethod.POST)
    public JSONResult page(@RequestBody CourseQuery query) {
        Page<Course> page = new Page<Course>(query.getPage(), query.getRows());
        EntityWrapper<Course> wrapper = new EntityWrapper<>();
        wrapper.like("name",query.getKeyword());
        page = courseService.selectPage(page,wrapper);
        return JSONResult.success(new PageList<Course>(page.getTotal(), page.getRecords()));
    }
}
